
	function  go(val,row){
		return "<a href='#'  class='button-delete button-danger  l-btn l-btn-small' onclick='deleterow(this)' ><span class='l-btn-left' ><span class='l-btn-text'>删除</span></span></a>";
	} 
	function getRowIndex(target){
		var tr = $(target).closest('tr.datagrid-row');
		return parseInt(tr.attr('datagrid-row-index'));
	}
	function deleterow(target){
		$.messager.confirm('Confirm','确定删除？',function(r){
			if (r){
				$('#stockHandover').datagrid('deleteRow', getRowIndex(target));
			}
		});
	}
$(document).ready(function () {		
	$('#stockHandover_datebox').datetimebox("setValue",formatterDate(new Date()));
		loadDatagrid();
		combobobxOnchange();
	})		
	function  loadDatagrid()
	{
			var columns=[[
			            {field:'productids',title:'产品ids', hidden:true},
						{field:'productname',title:'产品名称',width:150,align:'center'},
						{field:'typeids',title:'类别ids',hidden:true},
						{field:'productclass',title:'产品类别',width:100, align:'center'},
						{field:'numberonwork',title:'上班数量',width:100, align:'center',
							formatter: function(value,row,index)
							{	
							
								var numberonwork = row.numberonwork;
								if(numberonwork!=null){ return  numberonwork;}
								else{return row.numberonwork="0";}
							}
						},
						{field:'usenumber',title:'使用数量',width:100, align:'center',
							formatter: function(value,row,index)
							{
								var usenumber = row.usenumber;
								if(usenumber!=null){ return  usenumber;}
								else{return row.usenumber="0";}
							}
						},
						{field:'returnnumber',title:'退货数量',width:100, align:'center',
							formatter: function(value,row,index)
							{
								var returnnumber = row.returnnumber;
								if(returnnumber!=null){ return  returnnumber;}
								else{return row.returnnumber="0";}
							}
						},
						{field:'instorage',title:'入库',width:100, align:'center',
							formatter: function(value,row,index)
							{
								var instorage = row.instorage;
								if(instorage!=null){ return  instorage;}
								else{return row.instorage="0";}
							}
						},
						{field:'outstorage',title:'出库',width:100, align:'center',
							formatter: function(value,row,index)
							{
								var outstorage = row.outstorage;
								if(outstorage!=null){ return  outstorage;}
								else{return row.outstorage="0";}
							}
						},
						{field:'stock',title:'账面库存',width:100, align:'center',
							formatter: function(value,row,index)
							{
								var stock = row.stock;
								if(stock!=null){ return  stock;}
								else{return row.stock="0";}
							}
						},
						{field:'actuallynumber',title:'实际盘点',width:100, align:'center',
							editor:{type:'numberspinner',options:{min:0,
//								onChange : function(n){
//									diff(n);
//								}}},
								onSpinUp:function()
								{	
									var n=$(this).numberspinner('getValue');
									diff(null,n);
								},
				              onSpinDown:function()
			    				{
			    					var n=$(this).numberspinner('getValue');
			    					diff(null,n);
			    				}}},
								formatter: function(value,row,index)
								{
									var actuallynumber = row.actuallynumber;
									if(actuallynumber!=null){ return  value;}
									else{return row.actuallynumber="0";}
								}
						},
						{field:'difference',title:'差异',width:100, align:'center'
						},
						{field:'reasondifference',title:'差异原因',width:100, align:'center',editor:'textbox',
							formatter: function(value,row,index)
							{	
								if(value!=null){ return  value;}
								else{return row.reasondifference=" ";}
							}
						}
						]];
	
	
	
	
	$('#stockHandover').datagrid({
		cls: "theme-datagrid",
		singleSelect: true,
		rownumbers: true, //显示序号
		fitColumns:true,
		pagination: true,
		pageSize: 10,
		onClickRow: onClickRow,
		url: "bstockHandover/getStock",
		queryParams: {
			'_query.sshorganIds':  $("#organIds").val(),
			'_query.sshjjDate': $("#handoverJjDate").val(),
			'_query.stockorganIds': $("#organIds").val(),
			'_query.kcstarttime': $("#handoverJjDate").val(),
			'_query.kcendtime': formatterDate(new Date()),
			'_query.kcorganIds': $("#organIds").val(),
			'_query.xsstarttime': $("#handoverJjDate").val(),
			'_query.xsendtime': formatterDate(new Date()),
			'_query.xsorganIds': $("#organIds").val(),
			'_query.lcIds': $("#lcIds").val()
		},
	    method: 'post',
		collapsible: true,
		columns:columns,
		onAfterEdit:function(index,filed,value)
		{	
			var row=$("#stockHandover").datagrid("getRows")[index];
			diff(index,row.actuallynumber);
		}
		})
	}
	
	
//差异
	function diff(index,n)
	{	
		var row;
		if(index==null)
		{
			var rows = $("#stockHandover").datagrid("getRows");
			row = $('#stockHandover').datagrid('getSelected');
			index=$('#stockHandover').datagrid('getRowIndex', row);
			var d=parseInt(n)-parseInt(row.stock);
			$('#stockHandover').datagrid('updateRow', {
		        index: index,
		        row: {
		        	actuallynumber:n,
		        	difference:d
		        }
		    });
			$('#stockHandover').datagrid('beginEdit', index);
				
		}else
		{
			var row = $("#stockHandover").datagrid("getRows")[index];
			var d=parseInt(n)-parseInt(row.stock);
			$('#stockHandover').datagrid('updateRow', {
		        index: index,
		        row: {
		        	actuallynumber:n,
		        	difference:d
		        }
		    });
		}
		
		
	}
	function combobobxOnchange()
	{
		$("#jjr_select").combobox({		
			onChange: function () {	
				$('#jjr_password').textbox('setValue',"");
				$('#jjr_status').val('0');
				$("#jjr_yzcg").hide();
				$("#jjr_yzmm").show();
			}		
		});	
		$("#jbr_select").combobox({		
			onChange: function () {	
				$('#jbr_password').textbox('setValue',"");
				$('#jbr_status').val('0');
				$("#jbr_yzcg").hide();
				$("#jbr_yzmm").show();
				
			}		
		});	
		
	}
	
	
	
	/*验证密码
	 * 
	 */
	function  verifyPassword(obj)
	{	
		var  userIds;
		var  password;
		if(obj=='1')
		{
			userIds=$('#jjr_select').combobox("getValue");
			password=$('#jjr_password').textbox("getValue");
		}
		if(obj=='2')
		{
			userIds=$('#jbr_select').combobox("getValue");
			password=$('#jbr_password').textbox("getValue");
		}
			var url='/bstockHandover/verifyPassword';
			var data={"userIds":userIds,"password":password};
			var reData = common_ajax.ajaxFunc(url,data);
			if(reData == "200")
			{
				 $.slideMsg("密码验证成功", parent.$);
				 if(obj=='1'){ $('#jjr_status').val('1');$("#jjr_yzcg").show();$("#jjr_yzmm").hide();}
				 if(obj=='2'){ $('#jbr_status').val('1');$("#jbr_yzcg").show();$("#jbr_yzmm").hide();}
			}else
			{
				$.alert(parent.$, reData);
				 if(obj=='1'){ $('#jjr_status').val('0');$("#jjr_yzcg").hide();$("#jjr_yzmm").show();}
				 if(obj=='2'){ $('#jbr_status').val('0');$("#jbr_yzcg").hide();$("#jbr_yzmm").show();}
			}
		
		
	}
	
	/*保存*/
	function  save()
	{	
		var jjrStatus=$('#jjr_status').val();
		var jbrStatus=$('#jbr_status').val();
		if(jjrStatus=='0')
		{
			$.alert(parent.$,"请先验证交接人密码！");
		}else if(jbrStatus=='0')
		{
			$.alert(parent.$,"请先验证接班人密码！");
		}else if(jjrStatus=='1' && jbrStatus=='1')
		{
				$('#stockHandover').datagrid("endEdit", editIndex1);
				/*把所有数据都拼接到form表单里*/
				$("#allData").html("");
				var rows = $('#stockHandover').datagrid('getRows');
				for(var i=0;i<rows.length;i++)
				{	
					var rowIndex = $('#stockHandover').datagrid('getRowIndex', rows[i]);
					 $('#stockHandover').datagrid('endEdit', rowIndex);
					var productids = rows[i].productids;
					var typeids = rows[i].typeids;
					var numberonwork = rows[i].numberonwork;
					var usenumber = rows[i].usenumber;
					var instorage = rows[i].instorage;
					var outstorage = rows[i].outstorage;
					var stock = rows[i].stock;
					var actuallynumber = rows[i].actuallynumber;
					var difference = rows[i].difference;
					var reasondifference = rows[i].reasondifference;
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].productIds' value='"+productids+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].typeIds' value='"+typeids+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].initialQuantity' value='"+numberonwork+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].useNum' value='"+usenumber+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].storage' value='"+instorage+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].outgoing' value='"+outstorage+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].bookInventory' value='"+stock+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].physicalInventory' value='"+actuallynumber+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].difference' value='"+difference+"'/>");
					$("#allData").append("<input type='hidden' name='stockTransferList["+i+"].differenceCause' value='"+reasondifference+"'/>");
				}	
				var date=$('#stockHandover_datebox').datetimebox("getValue");
				if(date != "")
				{		
					    var url='/bstockHandover/saveStockHandover';
						var data=$('#allData_form').serialize();
						var reData = common_ajax.ajaxFunc(url,data);
						if(reData == "200")
						{
							 $.slideMsg("保存成功", parent.$);
							 parent.refresh();
						}else
						{
							$.alert(parent.$, reData);
						}
				}else
				{
					$.alert(parent.$,"请选择交接时间");
				}
		
		}
	}
	/*数据表格行编辑*/
	var editIndex1 = undefined;
	function endEditing(){
		if (editIndex1 == undefined){return true}
		if ($('#stockHandover').datagrid('validateRow', editIndex1)){
			$('#stockHandover').datagrid('endEdit', editIndex1);
				editIndex1 = undefined;
				return true;
		} else {
			return false;
		}
	}
	function onClickRow(index){
		if (editIndex1 != index){
			if (endEditing()){
				$('#stockHandover').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
					editIndex1 = index;
			} else {
				$('#stockHandover').datagrid('selectRow', editIndex1);
			}
		}
	}
/*数据表格行删除*/	
	
//	//得到当前日期
	formatterDate = function (date) {
		var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
		var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
		+ (date.getMonth() + 1);
		var hor = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
		};